import cv2
import numpy as np

def stitch_videos(video_paths):
    # 打开视频文件
    caps = [cv2.VideoCapture(path) for path in video_paths]
    
    # 创建视频写入对象
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output.avi', fourcc, 30.0, (1920, 1080))
    
    while True:
        frames = []
        for cap in caps:
            ret, frame = cap.read()
            if not ret:
                break
            frames.append(frame)
        
        if len(frames) != len(caps):
            break
        
        # 拼接帧
        stitched_frame = np.hstack(frames)
        
        # 写入输出视频
        out.write(stitched_frame)
    
    # 释放资源
    for cap in caps:
        cap.release()
    out.release()

# 使用示例
video_files = ['video1.mp4', 'video2.mp4']
stitch_videos(video_files)